<?php
class Users_Model extends CI_Model
{
    function __construct() 
    {
        parent::__construct();
    }
    private function _set_where_conditions($paras = array())
    {
        if (isset($paras['role_id']))
            $this->db->where('roles_id', $paras['role_id']);

        if (isset($paras['id']))
            $this->db->where(USERS.'.id', $paras['id']);

        if (isset($paras['username']))
            $this->db->where('username', $paras['username']);

        if (isset($paras['email']))
            $this->db->where('email', $paras['email']);

        if (isset($paras['active']) && $paras['active'] != '')
            $this->db->where('active', $paras['active']);

         if (isset($paras['user_id_except']))
            $this->db->where('id <>' . $paras['user_id_except']);

        if (isset($paras['keyword']))
        {
            $where  = "(    fullname    like'%" . $paras['keyword'] . "%'";
            $where .= " or  email       like'%" . $paras['keyword'] . "%'";
            $where .= " or  address     like'%" . $paras['keyword'] . "%'";
            $where .= " or  phone1      like'%" . $paras['keyword'] . "%'";
            $where .= " or  phone2      like'%" . $paras['keyword'] . "%'";
            $where .= " or  username    like'%" . $paras['keyword'] . "%')";
            $this->db->where($where);
        }
        
        if (isset($paras['limit']) && isset($paras['offset']))
            $this->db->limit($paras['limit'], $paras['offset']);
        else if (isset($paras['limit']))
            $this->db->limit($paras['limit']);
        if(isset($paras['order_by']))
            $this->db->order_by($paras['order_by']);
        else
            $this->db->order_by('joined_date desc');
    }
    function get_users($paras = array())
    {
        // select
        $this->db->select( USERS . '.*,
                            ' . ROLES  . '.role
                        ');
        //join users
        $this->db->join(ROLES, USERS . '.roles_id = ' . ROLES . '.id', 'left');
        
        $this->_set_where_conditions($paras);

        if (isset($paras['order']))
            $this->db->order_by($paras['order']);
        else
            $this->db->order_by('alias_name');

        $query = $this->db->get(USERS);
        if(isset($paras['last_row']))
            return $query->row(0);
        return $query->result();
    }

    function get_users_array($paras = array())
    {
        $users = $this->get_users($paras);
        $output = array();
        foreach($users as $user)
        {
            if (isset($paras['FULLNAME']))
                $output[$user->id] = $user->fullname;
            else
                $output[$user->id] = $user->alias_name;
        }
        return $output;
    }

    function get_users_combo($options = array())
    {
        // Default categories name
        if ( ! isset($options['combo_name']))
        {
            $options['combo_name'] = USERS;
        }

        if ( ! isset($options['extra']))
        {
            $options['extra'] = '';
        }

        $users = $this->get_users($options); // added options 2011-03-11

        $data_options = array();

        if (isset($options['ALL']))
            $data_options[DEFAULT_COMBO_VALUE] = 'Tất cả';

        foreach($users as $user)
        {
            if (isset($options['FULL']))
                $data_options[$user->id] = $user->fullname;
            else
                $data_options[$user->id] = $user->alias_name;
        }

        if ( ! isset($options[$options['combo_name']]))
        {
            $options[$options['combo_name']] = DEFAULT_COMBO_VALUE;
        }
        return form_dropdown($options['combo_name'], $data_options, $options[$options['combo_name']], $options['extra']);
    }

    function get_roles($options = array())
    {
        $this->db->where('id != ', -1);
        return $this->db->get(ROLES)->result();
    }
    
    function get_roles_combo($options = array())
    {
        if ( ! isset($options['combo_name']))
        {
            $options['combo_name'] = 'roles';
        }

        $data_options = array();

        $data_options[DEFAULT_COMBO_VALUE] = 'Tất cả';
        
        $roles = $this->get_roles($options);
        foreach($roles as $role)
        {
            $data_options[$role->id] = $role->role;
        }
        if ( ! isset($options[$options['combo_name']])|| $options[$options['combo_name']] == '')
        {
            $options[$options['combo_name']] = DEFAULT_COMBO_VALUE;
        }
        return form_dropdown($options['combo_name'], $data_options, $options[$options['combo_name']]);
    }

    function get_user_possition_combo($options = array())
    {
        if ( ! isset($options['combo_name']))
        {
            $options['combo_name'] = 'role_id';
        }

        if ( ! isset($options['extra']))
        {
            $options['extra'] = '';
        }

        $roles = $this->get_roles();

        $data_options = array();

        foreach($roles as $role)
        {
            if ((int)$role->id === DEFAULT_COMBO_VALUE)
                $data_options[$role->id] = 'Tất cả';
            else
                $data_options[$role->id] = $role->role;
        }

        if ( ! isset($options[$options['combo_name']]))
        {
            $options[$options['combo_name']] = DEFAULT_COMBO_VALUE;
        }

        return form_dropdown(
                $options['combo_name'],
                $data_options,
                $options[$options['combo_name']],
                $options['extra']
                );
    }

    function get_user_status_combo($options = array())
    {
        if ( ! isset($options['combo_name']))
        {
            $options['combo_name'] = 'active';
        }

        $data_options = array();

        $data_options[DEFAULT_COMBO_VALUE] = 'Tất cả';
        $data_options[ACCOUNT_ACTIVE] = 'Đang công tác';
        $data_options[ACCOUNT_DEACTIVE] = 'Đã nghỉ việc';

        if ( ! isset($options[$options['combo_name']]))
        {
            $options[$options['combo_name']] = DEFAULT_COMBO_VALUE;
        }
        return form_dropdown($options['combo_name'], $data_options, $options[$options['combo_name']]);
    }

    function add_user($data = array())
    {
        $this->db->insert(USERS, $data);
        return $this->db->insert_id();
    }

    function update_user($data = array())
    {
        if (isset($data['id']))
        {
            $this->db->where(USERS . '.id', $data['id']);
            $this->db->update(USERS, $data);
        }
    }

    function delete_user($users_id)
    {
        if (isset($users_id))
        {
            $this->db->where(USERS . '.id', $users_id);
            $this->db->delete(USERS);
        }
    }

    /**
     * @author Thế Cường
     * @date 11-5-2011
     * Lấy tất cả user theo check box
     */

    function get_user_checkbox($options = array())
    {
        $options['user_id_except'] = $this->phpsession->get('user_id');
        $users = $this->get_users($options);

        if ( ! isset($options['extra']))
        {
            $options['extra'] = '';
        }

        $data_options = array();
        $check_box_array_from = array();
        foreach($users as $user)
        {
            if (isset($options['FULL']))
                $data_options[$user->id] = $user->fullname;
            else
                $data_options[$user->id] = $user->alias_name;

            $checkbox_name = USERS. $user->id;

            $data = array(
            'name'        => $checkbox_name,
            'value'       => $user->id,
            'style'       => $options['extra']
            );

            $data['checked'] = ($options['selected_ids'][$checkbox_name] == 1) ? TRUE : FALSE;
            $check_box_array_from['checkbox_name'][] = form_checkbox($data) . $data_options[$user->id];
        }
        return $check_box_array_from['checkbox_name'];
    }

    function get_user_role_info($user_id)
    {
        $result = $this->db->select(array(USERS . '.id', ROLES . '.id as role_id'))
                ->join(ROLES,  ROLES . '.id = ' . USERS . '.roles_id', 'INNER')
                ->where(array(USERS . '.id' => $user_id))
                ->get(USERS);
        return $result->row_array();
    }
     public function get_users_count($options = array()) {
        return count($this->get_users($options));
    }
    
}
